package xfsale.service.impl.j2ee;

import xfsale.exception.AppException;
import xfsale.packet.BaseRequest;
import xfsale.packet.BaseResponse;

public abstract class BaseOperation extends BaseService {

    private BaseRequest request;        //服务请求
    private BaseResponse response;      //服务应答

    public BaseOperation(BaseRequest request, BaseResponse response) {
        super(request, response);

        this.request = request;
        this.response = response;
    }

    //初始化
    @Override
    protected void init() throws Exception {
        super.init();

        //初始化系统参数
        sys.initParam();
    }

    //检查输入参数
    @Override
    protected void checkData() throws Exception {
        super.checkData();

        //检查功能代码
        if(!request.has("serviceCode")) {
            throw new AppException("请求不包括serviceCode属性");
        }
        if (!request.getString("serviceCode").matches("^[0-9]{4}$")) {
            throw new AppException("功能代码不合法");
        }

        //检查地区代码
        if(!request.has("regionCode")) {
            throw new AppException("请求不包括regionCode属性");
        }
        if (!request.getString("regionCode").matches("^[0-9]{1,4}$")) {
            throw new AppException("地区代码不合法");
        }

        //检查网点代码
        if(!request.has("branchCode")) {
            throw new AppException("请求不包括branchCode属性");
        }
        if (!request.getString("branchCode").matches("^[0-9]{1,9}$")) {
            throw new AppException("网点代码不合法");
        }

        //检查销售网点代码
        if(!request.has("netCode")) {
            throw new AppException("请求不包括netCode属性");
        }
        if (!request.getString("netCode").matches("^[0-9]{1,9}$")) {
            throw new AppException("销售网点代码不合法");
        }

        //检查操作员编号
        if(!request.has("opCode")) {
            throw new AppException("请求不包括opCode属性");
        }
        if (!request.getString("opCode").matches("^[0-9]{1,8}$")) {
            throw new AppException("操作员编号不合法");
        }

        //检查受理方式
        if(!request.has("acceptMethod")) {
            throw new AppException("请求不包括acceptMethod属性");
        }
        if (!request.getString("acceptMethod").matches("^[01234]$")) {
            throw new AppException("受理方式不合法");
        }
    }

    //初始化
    @Override
    protected void load() throws Exception {
        super.load();
    }

    //检查功能权限
    @Override
    protected void checkPriv() throws Exception {
        super.checkPriv();

        //检查系统状态是否允许执行该业务
        priv.setSysStatus(sys.getStatus());
        priv.setServiceCode(request.getString("serviceCode"));
        priv.setAcceptMethod(request.getString("acceptMethod"));
        priv.checkSys();
    }

    //检查条件是否满足
    @Override
    protected void checkCondition() throws Exception {
        super.checkCondition();
    }

    //写入操作日志
    @Override
    protected void writeLog() throws Exception {

        //写入操作日志数据
        log.setServiceCode(request.getString("serviceCode"));
        log.setBusinessCode(request.getString("businessCode"));
        log.setAcceptMethod(request.getString("acceptMethod"));
        log.setRegionCode(request.getString("regionCode"));
        log.setBranchCode(request.getString("branchCode"));
        log.setNetCode(request.getString("netCode"));
        log.setOpCode(request.getString("opCode"));
        log.setOpDate(sys.getWorkDay());
        log.setOpTime(sys.getWorkTime());
        log.setIpAddr(null);
        log.setMacAddr(null);
        log.setReturnCode(response.getString("returnCode"));
        log.setReturnMsg(response.getString("returnMsg"));
        log.writeLog();
    }

    //功能处理
    @Override
    protected void process() throws Exception {
        super.process();
    }
}
